package com.five.wisdomEat.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.five.wisdomEat.entity.RestaurantOrders;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RestaurantOrdersMapper extends BaseMapper<RestaurantOrders> {
    //商家查看自己所有订单
    @Select("SELECT ro.*,p.p_name,os.os_name,b.b_name,s.s_name FROM restaurant_orders ro " +
            "INNER JOIN payment p ON ro.ro_paymentId=p.id " +
            "INNER JOIN order_state os ON ro.ro_stateId=os.id " +
            "INNER JOIN businessinfo b ON ro.ro_b_number = b.b_number " +
            "INNER JOIN studentinfo s ON ro.ro_s_number = s.s_number " +
            "WHERE ro.ro_b_number = #{bNumber} " +
            "ORDER BY ro_buytime desc")
    List<RestaurantOrders> selectROrderListByBusiness(String bNumber);

    //学生查看自己所有订单
    @Select("SELECT ro.*,p.p_name,os.os_name,b.b_name,s.s_name FROM restaurant_orders ro " +
            "INNER JOIN payment p ON ro.ro_paymentId=p.id " +
            "INNER JOIN order_state os ON ro.ro_stateId=os.id " +
            "INNER JOIN businessinfo b ON ro.ro_b_number = b.b_number " +
            "INNER JOIN studentinfo s ON ro.ro_s_number = s.s_number " +
            "WHERE ro.ro_s_number = #{sNumber} " +
            "ORDER BY ro_buytime desc")
    List<RestaurantOrders> selectROrderListByStudent(String sNumber);

    //添加订单
    @Insert("INSERT INTO restaurant_orders " +
            "(id,ro_number,ro_content,ro_count,ro_price,ro_paymentId,ro_stateId,ro_s_number,ro_buytime,ro_b_number,isValid) " +
            "VALUES (null,#{roNumber},#{roContent},#{roCount},#{roPrice},#{roPaymentId},#{roStateId}," +
            "#{roSNumber},#{roBuyTime},#{roBNumber},1)")
    Integer insertROrder(RestaurantOrders restaurantOrders);

    //根据订单编号修改订单
    @Update("UPDATE restaurant_orders SET ro_stateId=#{roStateId} WHERE ro_number = #{roNumber}")
    Integer updateROrder(RestaurantOrders restaurantOrders);

    //确认收货
    @Update("UPDATE restaurant_orders SET ro_stateId=3 WHERE ro_number = #{roNumber}")
    Integer confirmROrder(String roNumber);

    //确认出单
    @Update("UPDATE restaurant_orders SET ro_stateId=2 WHERE ro_number = #{roNumber}")
    Integer saleROrder(String roNumber);

    //根据订单编号删除订单
    @Delete("DELETE FROM restaurant_orders WHERE ro_number = #{roNumber}")
    Integer deleteROrder(String roNumber);

    //商家不定查询订单
    @Select("SELECT ro.*,p.p_name,os.os_name,b.b_name,s.s_name FROM restaurant_orders ro " +
            "INNER JOIN payment p ON ro.ro_paymentId=p.id " +
            "INNER JOIN order_state os ON ro.ro_stateId=os.id " +
            "INNER JOIN businessinfo b ON ro.ro_b_number = b.b_number " +
            "INNER JOIN studentinfo s ON ro.ro_s_number = s.s_number " +
            "WHERE ro.ro_b_number = #{roBNumber} " +
            "AND ro.ro_number LIKE CONCAT('%', #{roNumber}, '%')  " +
            "AND s.s_name LIKE CONCAT('%', #{sName}, '%')  " +
            "AND (ro.ro_stateId = #{roStateId} OR #{roStateId}<0) " +
            "AND (ro.ro_paymentId = #{roPaymentId} OR #{roPaymentId}<0) " +
            "ORDER BY ro_buytime desc")
    List<RestaurantOrders> selectROrderListByBusinessExample(RestaurantOrders restaurantOrders);

    //学生不定查询订单
    @Select("SELECT ro.*,p.p_name,os.os_name,b.b_name,s.s_name FROM restaurant_orders ro " +
            "INNER JOIN payment p ON ro.ro_paymentId=p.id " +
            "INNER JOIN order_state os ON ro.ro_stateId=os.id " +
            "INNER JOIN businessinfo b ON ro.ro_b_number = b.b_number " +
            "INNER JOIN studentinfo s ON ro.ro_s_number = s.s_number " +
            "WHERE ro.ro_s_number = #{roSNumber} " +
            "AND ro.ro_number LIKE CONCAT('%', #{roNumber}, '%')  " +
            "AND b.b_name LIKE CONCAT('%', #{bName}, '%')   " +
            "AND (ro.ro_stateId = #{roStateId} OR #{roStateId}<0) " +
            "AND (ro.ro_paymentId = #{roPaymentId} OR #{roPaymentId}<0) " +
            "ORDER BY ro_buytime desc")
    List<RestaurantOrders> selectROrderListByStudentExample(RestaurantOrders restaurantOrders);

    //查询订单数量
    @Select("SELECT COUNT(*) FROM restaurant_orders")
    Integer selectOrdersCount();

    //查询二维码是否存在
    @Select("SELECT * FROM restaurant_orders WHERE ro_number = #{roNumber}")
    RestaurantOrders selectQR(String roNumber);

    //使二维码失效
    @Update("UPDATE restaurant_orders SET ro_stateId = 3, isValid = 0  WHERE ro_number = #{roNumber}")
    Integer updateQR(String roNumber);
}
